package gunfire.cards;

import basemod.abstracts.CustomCard;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.DamageAction;
import com.megacrit.cardcrawl.actions.common.DamageAllEnemiesAction;
import com.megacrit.cardcrawl.actions.common.GainBlockAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.DamageInfo;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import gunfire.GunfireMod;
import gunfire.characters.Taizi;

import static gunfire.GunfireMod.makeCardPath;

public class BoneDragon extends CustomCard {

    public static final String ID = GunfireMod.makeID(BoneDragon.class.getSimpleName());
    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG = makeCardPath("BoneDragon.png");
    private static final CardRarity RARITY = CardRarity.UNCOMMON;
    private static final CardType TYPE = CardType.ATTACK;
    private static final CardTarget TARGET = CardTarget.ENEMY;
    public static final CardColor COLOR = Taizi.Enums.COLOR_GRAY;
    private static final int COST = 2;

    public BoneDragon() {
        super(ID, NAME, IMG, COST, DESCRIPTION, TYPE, COLOR, RARITY, TARGET);
        int size = 0;
        try {
            for(AbstractMonster m : AbstractDungeon.getCurrRoom().monsters.monsters){
                if(!m.isDead && !m.escaped){
                    size++;
                }
            }
        } catch (Exception e){
            size = 0;
        }
        this.baseBlock = this.baseDamage = 2 * size * size;
        this.isMultiDamage = true;
    }

    @Override
    public void upgrade() {
        if (!this.upgraded) {
            this.upgradeName();
            this.target = CardTarget.ALL_ENEMY;
            this.rawDescription = cardStrings.UPGRADE_DESCRIPTION;
            this.initializeDescription();
        }
    }

    @Override
    public void triggerWhenDrawn() {
        this.calculateCardDamage(AbstractDungeon.getRandomMonster());
    }

    @Override
    public void calculateCardDamage(AbstractMonster mo) {
        int size = 0;
        try {
            for(AbstractMonster m : AbstractDungeon.getCurrRoom().monsters.monsters){
                if(!m.isDead && !m.escaped){
                    size++;
                }
            }
        } catch (NullPointerException e){
            size = 0;
        }
        this.baseBlock = this.baseDamage = 2 * size * size;
        super.calculateCardDamage(mo);
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {
        this.addToBot(new GainBlockAction(p, p, this.block));
        if(this.upgraded){
            this.addToBot(new DamageAllEnemiesAction(p, this.multiDamage, DamageInfo.DamageType.NORMAL, AbstractGameAction.AttackEffect.BLUNT_HEAVY));
        } else {
            this.addToBot(new DamageAction(m, new DamageInfo(p, this.damage, DamageInfo.DamageType.NORMAL), AbstractGameAction.AttackEffect.BLUNT_LIGHT));
        }
    }

    @Override
    public AbstractCard makeCopy() {
        return new BoneDragon();
    }
}
